package util;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

public class XmlValidator {
	private static final String W3C ="http://www.w3.org/2001/XMLSchema";
	public static boolean validator(String xmlpath, String xsdpath){
		boolean flag = false;		
		try {
			SchemaFactory sf = SchemaFactory.newInstance(W3C);
			String str=Thread.currentThread().getContextClassLoader().getResource("").getPath();
			xsdpath = str + xsdpath;
			xmlpath = str + xmlpath;
			File f = new File(xsdpath);
			//System.out.println(xsdpath);		
			Schema schema = sf.newSchema(f);
			Validator v = schema.newValidator();			
			Source s = new StreamSource(xmlpath);
			v.validate(s);
			flag = true;
			//System.out.println("the schema validate is OK....");
		} catch (SAXException e) {
			System.out.println("the schema validate failed....");
			e.printStackTrace();
		}catch (IOException e) {
			System.out.println("create exception in IO....");
			e.printStackTrace();
		}
		return flag;
	}
//	public static void main(String[] args){
//		String xmlPath = "src//database.conf.xml";
//		String xsdPath = "src//database.conf.xsd";
//		if(validator(xmlPath,xsdPath)){
//			System.out.println("the schema validate is OK....");
//		}
//	}
}
